package scct.zhang.dao;

import scct.zhang.model.Conversation;
import scct.zhang.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConversationDao {
    public Conversation login(String username, String contactname, String lasttime) {
        Conversation conversation = null;
//    获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_conversation where username=? and contactname=? and lasttime=?";
//    预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setString(1,username);
            pstmt.setString(2,contactname);
            pstmt.setString(3, lasttime);
//        查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String username2 = rs.getString("username");
                String contactname2 = rs.getString("contactname");
                String lasttime2 = rs.getString("lasttime");
                conversation = new Conversation(id,username2,contactname2,lasttime2);

            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conversation;
    }

    /**
     * @param username
     * @param contactname
     * @param lasttime
     * @return 用户对象
     */

//    添加联系人
    public boolean addConversation(String username, String contactname,String lasttime) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_conversation values(null,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(3,lasttime);
            pstmt.setString(2,contactname);
            pstmt.setString(1,username);
            result = pstmt.executeUpdate();
        } catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result == 1;
    }

    public boolean isDupname(int id){
        int count = 0;// 查到联系人的数量
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_conversation where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                count =1;
            }
//            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count>0;
    }
    public List<Conversation> getAllConversation() {
        Connection conn = DBUtils.getConnection();
        List<Conversation> conversations = new ArrayList<>();

        String sql = "select * from sm_conversation";
        try {
            PreparedStatement pstme = conn.prepareStatement(sql);
            ResultSet rs = pstme.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String contactname = rs.getString("contactname");
                String lasttime = rs.getString("lasttime");
                conversations.add(new Conversation(id,username,contactname,lasttime));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conversations;
    }

    public Conversation getConversationId(int id){
        Conversation conversation = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_conversation where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                String username = rs.getString("username");
                String contactname = rs.getString("contactname");
                String lasttime = rs.getString("lasttime");
                conversation = new Conversation(id,username,contactname,lasttime);

            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conversation;
    }

    public boolean deleteConversation(int id) {
        int num = 0;
        Connection conn = DBUtils.getConnection();
        String sqlStr = "delete from sm_conversation where id = ?";
        try{
            PreparedStatement pstmt = conn.prepareStatement(sqlStr);
            pstmt.setInt(1,id);
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num > 0;
    }
    public boolean updateConversation(int id,String username, String contactname,String lasttime){
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_conversation set username=?,contactname=?,lasttime=? where id = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(3,lasttime);
            pstmt.setString(2,contactname);
            pstmt.setString(1,username);
            pstmt.setInt(4,id);
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }
//    public ArrayList<Conversation> getConversationByName(String name) {
//        ArrayList<Conversation> conversations = new ArrayList<>();
//        Connection conn = DBUtils.getConnection();
//        String sql = "select * from sm_conversation where name like '%" + name + "%'";
//        try {
//            PreparedStatement pstmt = conn.prepareStatement(sql);
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                int id = rs.getInt("id");
//                String username = rs.getString("username");
//                String contactname = rs.getString("contactname");
//                String lasttime = rs.getString("lasttime");
//                conversations.add(new Conversation(id,username,contactname,lasttime));
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return conversations;
//    }

}
